package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryConfOptionListReq;
import com.sg.dto.base.component.req.QueryConfOptionDetailReq;
import com.sg.dto.base.component.req.QueryConfOptionListReq;
import com.sg.entity.OmsConfOption;
import com.sg.service.base.component.MOmsConfOptionService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfOptionMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfOptionServiceImpl
        extends ServiceImpl<OmsConfOptionMapper, OmsConfOption>
        implements MOmsConfOptionService {

    @Autowired
    OmsConfOptionMapper omsConfOptionMapper;

    /**
     * 2-3-02批量新增配置选项[2153]
     * gen by moon at 10/10/2022, 1:18:38 AM
     */
    @Trace(operationName = "2-3-02批量新增配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddConfOption(List<OmsConfOption> omsConfOption) {

        if (omsConfOption == null || omsConfOption.size() == 0) {
            return false;
        }

        return super.saveBatch(omsConfOption.stream().map(item -> {
            item.setConfOptionId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsTemporaryLock("TRUE");
            item.setSubjectLifeCycle("EDITING");
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-3-01批量删除配置选项[2063]
     * gen by moon at 10/10/2022, 1:18:37 AM
     */
    @Trace(operationName = "2-3-01批量删除配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfOption(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-01批量修改配置选项[2043]
     * gen by moon at 10/10/2022, 1:18:36 AM
     */
    @Trace(operationName = "2-3-01批量修改配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateConfOption(List<OmsConfOption> omsConfOption) {

        if (omsConfOption == null || omsConfOption.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfOption);

    }

    /**
     * 2-3-01批量查询配置选项列表[1998]
     * gen by moon at 5/3/2023, 10:15:13 AM
     */
    @Trace(operationName = "2-3-01批量查询配置选项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfOption> batchQueryConfOptionList(BatchQueryConfOptionListReq reqDto) {

        try {
            QueryWrapper<OmsConfOption> queryWrapper = new QueryWrapper<>();

            if (reqDto.getConfOptionList() == null || reqDto.getConfOptionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_option_id", reqDto.getConfOptionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfOption.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfOption.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfOption.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfOption>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-01查询配置选项列表[1913]
     * gen by moon at 10/10/2022, 1:18:35 AM
     */
    @Trace(operationName = "2-3-01查询配置选项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfOption> queryConfOptionList(QueryConfOptionListReq reqDto) {

        try {
            QueryWrapper<OmsConfOption> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfOption.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfOption.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfOption.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfOption>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-01查询配置选项详情[1912]
     * gen by moon at 10/10/2022, 1:18:34 AM
     */
    @Trace(operationName = "2-3-01查询配置选项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfOption queryConfOptionDetail(QueryConfOptionDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfOption> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfOption> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfOption retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-01删除配置选项[1911]
     * gen by moon at 10/10/2022, 1:18:34 AM
     */
    @Trace(operationName = "2-3-01删除配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfOption(String confOptionId) {

        return super.removeById(confOptionId);
    }

    /**
     * 2-3-01修改配置选项[1910]
     * gen by moon at 10/10/2022, 1:18:33 AM
     */
    @Trace(operationName = "2-3-01修改配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfOption(OmsConfOption omsConfOption) {
        omsConfOption.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfOption.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsConfOption);
    }

    /**
     * 2-3-01新增配置选项[1909]
     * gen by moon at 10/10/2022, 1:18:32 AM
     */
    @Trace(operationName = "2-3-01新增配置选项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfOption(OmsConfOption omsConfOption) {
        omsConfOption.setConfOptionId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfOption.setSubjectLifeCycle("EDITING");
        omsConfOption.setIsArchive("FALSE");
        omsConfOption.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfOption.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfOption != null && omsConfOption.getSubjectLifeCycle() != null && omsConfOption.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfOption.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfOption.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfOption.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfOption != null && omsConfOption.getSubjectLifeCycle() != null && omsConfOption.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfOption.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfOption.setIsValid("FALSE");
        omsConfOption.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfOption.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfOption)) {
            return omsConfOption.getConfOptionId();
        } else {
            return "-1";
        }

    }
}
